package models

import (
	"time"

	"github.com/astaxie/beego"
)

type WakeOnLan struct {
	Id         int
	Name       string    `orm:"column(name)"`
	Data       string    `orm:"column(data)"`
	AddTime    time.Time `orm:"auto_now_add;column(add_time);type(datetime)"`
	UpdateTime time.Time `orm:"auto_now;column(update_time);type(datetime)"`
}

func GetWakeOnlanByName(name string) *WakeOnLan {
	wol := WakeOnLan{Name: name}
	// 三个返回参数依次为：是否新创建的，对象 Id 值，错误
	if created, id, err := Orm.ReadOrCreate(&wol, "Name"); err == nil {
		if created {
			beego.Debug("New Insert an object. Id:", id)
		} else {
			//fmt.Println("Get an object. Id:", id)
		}
	}
	return &wol
}

func (wol *WakeOnLan) Update() bool {
	n, _ := Orm.Update(wol)
	if n > 0 {
		return true
	}
	return false
}
